package no.uio.inf5750.j2mewsclient;

import org.xml.sax.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import java.io.*;
import javax.microedition.io.*;
import no.uio.inf5750.j2mewsclient.*;
import java.util.*;

/**
 * Parser handler class to parse the XML input stream.
 */
class MyEventHandler extends DefaultHandler {

	private SimpleMIDlet simplemidlet;
	StringBuffer sb = new StringBuffer();
	
	String msg;
	public MyEventHandler(SimpleMIDlet mymidlet){
	simplemidlet = mymidlet;
	}
	
	boolean insideDatasetNameElement;

  // Private members needed to parse the XML document
  //  private boolean parsingInProgress; // keep track of parsing
//  private Stack qNameStack = new Stack(); // keep track of QName
//  private Vector myElement2s = new Vector(); // keep track of element2

  // XML TAGS
/*   private static final String ELEMENT1 = "element1";
  private static final String ELEMENT2 = "element2";
  private static final String ATTRIBUTE1 = "attribute1"; */

  /**
   * Start of document processing.
   * @throws org.xml.sax.SAXException is any SAX exception, 
   * possibly wrapping another exception.
  */
  public void startDocument() 
          throws SAXException {
//      parsingInProgress = true;
//      qNameStack.removeAllElements();
//      myElement2s.removeAllElements();

	System.out.println("Start document");

	//	Cheating here: writing out a simple xml declaration	
//	System.out.println("<?xml version=\"1.0\"?>");
	
  }

  /**
   * End of document processing.
   * @throws org.xml.sax.SAXException is any SAX exception, 
   * possibly wrapping another exception.
   */
  public void endDocument() 
          throws SAXException {
//      parsingInProgress = false;
      // We have encountered the end of the document. Do any processing that is desired, 
      //   for example dump all collected element2 values.
//      for (int i=0; i<myElement2s.size(); i++) {
//          MyElement2 o = (MyElement2) myElement2s.elementAt(i);
//          o.dump();
//      }
//	msg = "I am the DHIS";

	msg = sb.toString();
	simplemidlet.alert("The datasets include; " + msg);
	//System.out.println("The msg is : " + msg);
	System.out.println("End document");
	}

  /**
   * Process the new element.
   * @param uri is the Namespace URI, or the empty string if the element 
   * has no Namespace URI or if Namespace processing is not being performed.
   * @param localName is the The local name (without prefix), or the empty 
   * string if Namespace processing is not being performed.
   * @param qName is the qualified name (with prefix), or the empty string 
   * if qualified names are not available.
   * @param attributes is the attributes attached to the element. If there 
   * are no attributes, it shall be an empty Attributes object.
   * @throws org.xml.sax.SAXException is any SAX exception, 
   * possibly wrapping another exception.
   */
  public void startElement(String uri, String localName, String qName, Attributes attributes) 
          throws SAXException {
//      if (ELEMENT1.equals(qName)) {
        // ELEMENT1 has an attribute, get it by name
//          String attribute1 = attributes.getValue(ATTRIBUTE1);
          // Do something with the attribute
          // ...
//      } else {
//          if (ELEMENT2.equals(qName)) {
              // Keep track of the value of element2
//              MyElement2 o = new MyElement2();
//              myElement2s.addElement(o);
//          }
//      }
      // Keep track of QNames
//      qNameStack.push(qName);
	System.out.println("Starting element " + localName + qName);
	if (qName.equals("name")) {
      insideDatasetNameElement = true;
    } 
	
/* 	else if (localName.equals("name")) {
      insideNameElement = true;
    } else if (localName.equals("phone")) {
      insidePhoneElement = true;
    } else if (localName.equals("email")) {
      insideEmailElement = true;
    } */

  }

  /**
   * Process the character data for current tag.
   * @param ch are the element's characters.
   * @param start is the start position in the character array.
   * @param length is the number of characters to use from the 
   * character array.
   * @throws org.xml.sax.SAXException is any SAX exception, 
   * possibly wrapping another exception.
   */
  public void characters(char[] ch, int start, int length)
          throws SAXException {
			String str = new String(ch, start, length);
/* 		  for (int i = start; i < start + length; i++)
			 str += ch[i]; */
			
			 
			 if (insideDatasetNameElement){
				System.out.println("I am inside name element adding characters");
				sb.append(str + ",   ");
			} else {
			 System.out.println("I am outside name element?");
			}
			// new String(ch, start, length)
			
			
/*       String qName;
      String chars = new String(ch, start, length);
      // Get current QName
      qName = (String) qNameStack.peek();
      if (ELEMENT1.equals(qName)) {
          // Nothing to process
      } else if (ELEMENT2.equals(qName)) {
          // Keep track of the value of element2
          if (myElement2s.size() > 0) {
              MyElement2 o = (MyElement2) myElement2s.lastElement();
              o.setValue(chars);
          }
      } */
  }

  /**
   * Process the end element tag.
   * @param uri is the Namespace URI, or the empty string if the element 
   * has no Namespace URI or if Namespace processing is not being performed.
   * @param localName is the The local name (without prefix), or the empty 
   * string if Namespace processing is not being performed.
   * @param qName is the qualified name (with prefix), or the empty 
   * string if qualified names are not available.
   * @throws org.xml.sax.SAXException is any SAX exception, 
   * possibly wrapping another exception.
   */
  public void endElement(String uri, String localName, String qName)
          throws SAXException {
/*       // Pop QName, since we are done with it
      qNameStack.pop();
      if (ELEMENT1.equals(qName)) {
          // We have encountered the end of ELEMENT1
          // ...
      } else {
          if (ELEMENT2.equals(qName)) {
              // We have encountered the end of an ELEMENT2
              // ...
          }
   } 
  */
  
  	if (qName.equals("name")) {
      insideDatasetNameElement = false;
    } 
}
}
